from marshmallow import Schema, fields, validate, pre_load

class Auth(Schema):
    email = fields.Str(required=True, validate=validate.Email())
    password = fields.Str(required=True, validate=validate.Length(min=6, max=36))

auth_schema = Auth()

class User(Schema):
    id = fields.Int(dump_only=True)
    role = fields.Str(validate=validate.OneOf(['admin', 'student', 'professor']), dump_only=True)
    name = fields.Str(required=True, validate=validate.Length(min=2, max=36))
    email = fields.Str(required=True, validate=validate.Email())
    password = fields.Str(required=True, validate=validate.Length(min=6, max=36), load_only=True)
    created_at = fields.DateTime(required=True, dump_only=True)

    @pre_load
    def process_input(self, data, **kwargs):
        data['email'] = data['email'].lower().strip()
        return data

user_schema = User()

class Course(Schema):
    name = fields.Str(required=True)
    about = fields.Str(required=True)
    professor_id = fields.Int(required=True)
    lessons = fields.Int(required=True)
    school_year = fields.Int(required=True)
    trimester = fields.Int(required=True)

course_schema = Course()

class Timetable(Schema):
    def get_course_state(self, obj):
        return obj.state.name

    id = fields.Int(dump_only=True)
    course_id = fields.Int(dump_only=True)
    title = fields.Str(dump_only=True)
    about = fields.Str(dump_only=True)
    open_time = fields.DateTime(dump_only=True)
    start_time = fields.DateTime(dump_only=True)
    end_time = fields.DateTime(dump_only=True)
    place = fields.Str(dump_only=True)
    state = fields.Method('get_course_state')
    number_of_sign_in = fields.Int(dump_only=True)

timetable_schema = Timetable()
